package com.ebs.md.fxmonitor;

/**
 * Perforce Revision:
 *  $File: //depot/MarketData/fxmonitor/main/src/main/java/com/ebs/md/fxmonitor/AppInfo.java $
 *  $Revision: #6 $  $Change: 115590 $  $DateTime: 2012/10/11 12:27:52 $  $Author: Arik.Sher $
 */

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.util.Assert;

import com.ebs.commons.logger.MessageText;
import com.ebs.commons.util.EbsUtilities;


public class AppInfo implements AppInfoManagement {
    private final Date m_startDate;
    private String m_ip;
    private String m_port;
    private String m_name;
    private String m_id;
    private String m_description;
    private String m_version;
    private final String m_host;
    final static Logger s_logger = Logger.getLogger(AppInfo.class);
    private final static String APP_NAME = "FxMonitor";
    private List<String> m_messageFileNames;


    public AppInfo() {
        m_startDate = new Date();
        m_host = resolveHost();
    }

    public Date getStartDate() {
        return m_startDate;
    }

    public String getDescription() {
        return m_description;
    }

    public void setDescription(String description) {
        m_description = description;
    }

    public String getName() {
        return m_name;
    }

    public void setIp(String ip) {
        m_ip = ip;
    }

    public void setPort(String port) {
        m_port = port;
    }

    public String getVersion() {
        return m_version;
    }

    public void setVersion(String version) {
        m_version = version;
    }

    public String getId(){
       return m_id;
    }

    public void init(){
    	Assert.notEmpty(m_messageFileNames, "messageFileNames should not be empty");

    	loadMessages();

    	m_id = createAppId(m_ip, m_port);
    	m_name = APP_NAME + " " + m_id;
    }

    protected void loadMessages() {
    	for(String nextFileName : m_messageFileNames)
    	{
    		MessageText.addMessages(getClass().getResourceAsStream("/" + nextFileName));
    	}            	  
    	
    }
    
    private static String createAppId(String host, String port){
        String appId;
        if(EbsUtilities.isStringEmpty(host)){
            try {
                host = InetAddress.getLocalHost().getHostAddress();
            }
            catch (UnknownHostException ex) {//no ip - bizarre... use local host
                s_logger.error(new MessageText("NETLINK_0400010", APP_NAME));
                host = "127.0.0.1";
            }
        }
        appId = host + ":" + port;
        s_logger.info(new MessageText("NETLINK_0400011", APP_NAME, appId));
        return appId;
    }

    public String getHost() {
        return m_host;
    }

    public String getPort() {
        return "Not Applicable";
    }

    public void setMessageFileNames(List<String> messageFileNames)
    {
    	m_messageFileNames = messageFileNames;
    }
    
    public List<String> getMessageFileNames()
    {
    	return m_messageFileNames;
    }

    
    private String resolveHost() {
        String      hostName;
        InetAddress address;

        try {
            address     = InetAddress.getLocalHost();
            hostName    = address.getCanonicalHostName();
        }
        catch (UnknownHostException ex) {
            hostName    = "Unknown!";
        }

        return hostName;
    }

}
